/**
* Description - This trigger is executed if new account records are
                created by integrtaion user or a record owned by
                integration user is being updated
*
* Author      - Sayeed Faruqi
* Comapny     - Cognizant Technology Solutions
* Client      - Cisco (Linksys)
* Last Update - Feb, 2010
**/
trigger assignmentRules on Account (before insert, before update) {

     /** Begin Ggaurd code
     LINKSYS_SYS_PARAM__c sysParamObj = [Select value__c from linksys_sys_param__c
                                         WHERE   param_name__c = 'assignmentRules' ];
      string canRun = sysParamObj.VALUE__C.toUpperCase();
       if (canRun != 'ENABLED'){ 
        return;
       }
    **/
    
    // If any account record has POS data and type = 'Registered' then don't process the batch
    for (account aCheck : trigger.new){
        if (aCheck.POS_Year_to_Date__c !=  NULL && aCheck.type =='Registered'){
            return;
        }
    }
     
     for (account a : trigger.new) {
          boolean namedAccount = false;
          boolean ROWAccount = false;
          boolean USAccount = false;
          
          named_account_assignments__c [] assigneeNA = [SELECT country__c,customer_name__c, ownerid
                          FROM  named_account_assignments__c
                           WHERE customer_name__c = :a.name
                            AND country__c = :a.billingCountry
                             LIMIT 1];
                
          if (assigneeNA.size()> 0) {
              //assign the owner Id for the named account
              
              system.debug ('Named Account Owner Id is: '+ assigneeNA[0].id);
              a.ownerId =  assigneeNA[0].ownerid;
              namedAccount = true;
          }
          
          // Set the owner Id for US Accounts
          if (a.billingcountry =='United States' && !namedAccount){
              account_assignment__c [] assigneeUS = [SELECT country__c, state_province__c, ownerId
                                FROM account_assignment__c
                                 WHERE country__c = :a.billingCountry
                                  AND state_province__c = :a.billingstate
                                   LIMIT 1];
               if (assigneeUS.size() >0){
                  system.debug ('Account OwnerUS Id is: '+ assigneeUS[0].id);
                  a.ownerId = assigneeUS[0].ownerid;  //set account ownerId
                  USAccount = true;
               }
           }
           // Set the owner Id for ROW Accounts, find based on country field
           if (a.billingcountry <>'United States' && !namedAccount && !USAccount){
              account_assignment__c [] assigneeROW = [SELECT country__c, ownerId
                                FROM account_assignment__c
                                 WHERE country__c = :a.billingCountry
                                  LIMIT 1];
              if (assigneeROW.size() > 0){
                  system.debug ('Account OwnerROW Id is: '+ assigneeROW[0].id);
                  a.ownerId = assigneeROW[0].ownerid;  //set account ownerId
                  ROWAccount = true;
              }
           }
       }
   }